﻿<%@ Page Title="" Language="C#" MasterPageFile="~/site.Master" AutoEventWireup="true" CodeBehind="DanhMucSanPham.aspx.cs" Inherits="WebComputers.DanhMucSanPham" %>
<%@ MasterType VirtualPath="~/site.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Middle_Content" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Main_Content" runat="server">
    <h1>
        <asp:Label runat="server" ID="lblTenLoai"></asp:Label>
    </h1>
    <div id="divContent">
        <asp:Repeater runat="server" ID="rptDanhMucHangHoa" OnItemDataBound="rptDanhMucHangHoa_ItemDataBound">
            <ItemTemplate>
                <div <%# ((Container.ItemIndex+1) % 3) == 0 ? "class='product_box no_margin_right'" : "class='product_box'" %>>
                    <a class="noClass1" href="ChiTietSanPham.aspx?id=<%# Eval("iDanhMucHangHoa_ID") %>">
                        <img src="<%# FillImage(Eval("sHinhDaiDien").ToString()) %>" alt="" style="width: 200px; height: 150px;" />
                    </a>
                    <h3><%# Eval("sTenHangHoa") %></h3>
                    <p class="product_price"><%# string.Format("{0:n0}",Eval("dGiaBan")) %> VNĐ</p>
                    <a class="add_to_cart" onclick="AddToCart(<%# Eval("iDanhMucHangHoa_ID") %>)">Thêm vào giỏ</a>
                    <a class="detail" href="ChiTietSanPham.aspx?id=<%# Eval("iDanhMucHangHoa_ID") %>">Chi tiết</a>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    <div class="cleaner"></div>
    <div style="text-align:center;">
        <img src="Images/loading.gif" id="loader" alt="" style="display:none;" />
    </div>
    <asp:HiddenField runat="server" ID="hdfPageTotal" Value="0" />
    <asp:HiddenField runat="server" ID="hdfLoaiSanPham_ID" Value="0" />
    <script>
        var pageIndex = 1;
        var pageCount = $("#Main_Content_hdfPageTotal").val();
        var loaiSanPham_ID = $("#Main_Content_hdfLoaiSanPham_ID").val();
        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                GetRecords();
            }
        });
        function GetRecords() {
            pageIndex++;
            if (pageIndex <= pageCount) {
                $("#loader").show();
                $.ajax({
                    type: "POST",
                    url: "DanhMucSanPham.aspx/GetProduct",
                    data: '{pageIndex: ' + pageIndex + ', loaiSanPham_ID: '+ loaiSanPham_ID +'}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    },
                    error: function (response) {
                        alert(response.d);
                    }
                });
            }
        }
        function OnSuccess(response) {            
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var count = 1;
            //pageCount = parseInt(xml.find("PageCount").eq(0).find("PageCount").text());
            var products = xml.find("DanhMucHangHoa");
            products.each(function () {
                var product = $(this);
                var div = $("#divContent div").eq(0).clone(true);
                if ((count % 3) == 0) {
                    div.addClass("no_margin_right");
                }
                $("img", div).attr('src', product.find("sHinhDaiDien").text());
                $("h3", div).html(product.find("sTenHangHoa").text());
                $(".product_price", div).html(product.find("dGiaBan").text()+" VNĐ");
                $(".detail", div).attr('href', 'ChiTietSanPham.aspx?id=' + product.find("iDanhMucHangHoa_ID").text());
                $(".noClass1", div).attr('href', 'ChiTietSanPham.aspx?id=' + product.find("iDanhMucHangHoa_ID").text());
                $(".add_to_cart", div).attr('onclick', 'AddToCart(' + product.find("iDanhMucHangHoa_ID").text() + ')');
                $("#divContent").append(div);
                count++;
            });
            $("#loader").hide();
        }
    </script>
    <script>
        function AddToCart(id)
        {
            $.ajax({
                type: "POST",
                url: "DanhMucSanPham.aspx/AddToCart",
                data: '{id: ' + id + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: ReloadCart,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });

        }

        function ReloadCart(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            $("#lblSlg").html(xml.find("TongSoLuong"));
            $("#lblTTien").html(xml.find("TongTien"));
        }
    </script>
</asp:Content>
